home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 15 / Amiga Plus Leser CD 15.iso / Tools / Development / MosaicSRC / src / picread.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-13  |  7.9 KB  |  246 lines

  1. /****************************************************************************
  2.  * NCSA Mosaic for the X Window System                                      *
  3.  * Software Development Group                                               *
  4.  * National Center for Supercomputing Applications                          *
  5.  * University of Illinois at Urbana-Champaign                               *
  6.  * 605 E. Springfield, Champaign IL 61820                                   *
  7.  * mosaic@ncsa.uiuc.edu                                                     *
  8.  *                                                                          *
  9.  * Copyright (C) 1993, Board of Trustees of the University of Illinois      *
  10.  *                                                                          *
  11.  * NCSA Mosaic software, both binary and source (hereafter, Software) is    *
  12.  * copyrighted by The Board of Trustees of the University of Illinois       *
  13.  * (UI), and ownership remains with the UI.                                 *
  14.  *                                                                          *
  15.  * The UI grants you (hereafter, Licensee) a license to use the Software    *
  16.  * for academic, research and internal business purposes only, without a    *
  17.  * fee.  Licensee may distribute the binary and source code (if released)   *
  18.  * to third parties provided that the copyright notice and this statement   *
  19.  * appears on all copies and that no charge is associated with such         *
  20.  * copies.                                                                  *
  21.  *                                                                          *
  22.  * Licensee may make derivative works.  However, if Licensee distributes    *
  23.  * any derivative work based on or derived from the Software, then          *
  24.  * Licensee will (1) notify NCSA regarding its distribution of the          *
  25.  * derivative work, and (2) clearly notify users that such derivative       *
  26.  * work is a modified version and not the original NCSA Mosaic              *
  27.  * distributed by the UI.                                                   *
  28.  *                                                                          *
  29.  * Any Licensee wishing to make commercial use of the Software should       *
  30.  * contact the UI, c/o NCSA, to negotiate an appropriate license for such   *
  31.  * commercial use.  Commercial use includes (1) integration of all or       *
  32.  * part of the source code into a product for sale or license by or on      *
  33.  * behalf of Licensee to third parties, or (2) distribution of the binary   *
  34.  * code or source code to third parties that need it to utilize a           *
  35.  * commercial product sold or licensed by or on behalf of Licensee.         *
  36.  *                                                                          *
  37.  * UI MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR   *
  38.  * ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED          *
  39.  * WARRANTY.  THE UI SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY THE    *
  40.  * USERS OF THIS SOFTWARE.                                                  *
  41.  *                                                                          *
  42.  * By using or copying this Software, Licensee agrees to abide by the       *
  43.  * copyright law and all other applicable laws of the U.S. including, but   *
  44.  * not limited to, export control laws, and the terms of this license.      *
  45.  * UI shall have the right to terminate this license immediately by         *
  46.  * written notice upon Licensee's breach of, or non-compliance with, any    *
  47.  * of its terms.  Licensee may be held legally responsible for any          *
  48.  * copyright infringement that is caused or encouraged by Licensee's        *
  49.  * failure to abide by the terms of this license.                           *
  50.  *                                                                          *
  51.  * Comments and questions are welcome and can be sent to                    *
  52.  * mosaic-x@ncsa.uiuc.edu.                                                  *
  53.  ****************************************************************************/
  54.  
  55. #include "includes.h" /* This must be included for XtoI.h */
  56. #include "XtoI.h" /* This must be included before HTML.h */
  57. #include "HTML.h"
  58. #include "mosaic.h"
  59.  
  60.  
  61. #define DEF_BLACK       BlackPixel(dsp, DefaultScreen(dsp))
  62. #define DEF_WHITE       WhitePixel(dsp, DefaultScreen(dsp))
  63. #define    MAX_LINE    81
  64.  
  65.  
  66. extern unsigned char *ReadGIF();
  67. extern unsigned char *ReadXpm3Pixmap();
  68.  
  69.  
  70. unsigned char nibMask[8] = {
  71.     1, 2, 4, 8, 16, 32, 64, 128
  72. };
  73.  
  74.  
  75.  
  76. unsigned char *ReadXbmBitmap(FILE *fp, char *datafile, int *w, int *h,
  77.                  int *colrs)
  78. {
  79.     char line[MAX_LINE], name_and_type[MAX_LINE];
  80.     char *t;
  81.     char *t2;
  82.     unsigned char *ptr, *dataP;
  83.     int bytes_per_line, version10p, raster_length, padding;
  84.     int i, bytes, temp, value;
  85.     int Ncolors, charspp, xpmformat;
  86.         static unsigned long fg_pixel, bg_pixel;
  87.         static int done_fetch_colors = 0;
  88. //        extern XColor fg_color, bg_color;
  89. //        extern Widget view;
  90. //        extern int Vclass;
  91.     int blackbit;
  92.     int whitebit;
  93.  
  94.     *w = 0;
  95.     *h = 0;
  96.     Ncolors = 0;
  97.     charspp = 0;
  98.     xpmformat = 0;
  99.     for ( ; ; )
  100.     {
  101.         if (!(fgets(line, MAX_LINE, fp)))
  102.             break;
  103.         if (strlen(line) == (MAX_LINE - 1))
  104.         {
  105.             return((unsigned char *)NULL);
  106.         }
  107.         if (sscanf(line, "#define %s %d", name_and_type, &value) == 2)
  108.         {
  109.             if (!(t = strrchr(name_and_type, '_')))
  110.                 t = name_and_type;
  111.             else
  112.                 t++;
  113.             if (!strcmp("width", t))
  114.                 *w= value;
  115.             if (!strcmp("height", t))
  116.                 *h= value;
  117.             if (!strcmp("ncolors", t))
  118.                 Ncolors = value;
  119.             if (!strcmp("pixel", t))
  120.                 charspp = value;
  121.             continue;
  122.         }
  123.         if (sscanf(line, "static short %s = {", name_and_type) == 1)
  124.         {
  125.             version10p = 1;
  126.             break;
  127.         }
  128.         else if (sscanf(line,"static char * %s = {",name_and_type) == 1)
  129.         {
  130.             xpmformat = 1;
  131.             if (!(t = strrchr(name_and_type, '_')))
  132.                 t = name_and_type;
  133.             else
  134.                 t++;
  135.             if ((t2 = strchr(name_and_type, '[')) != NULL)
  136.                 *t2 = '\0';
  137.             if (!strcmp("mono", t))
  138.                 continue;
  139.             else
  140.                 break;
  141.         }
  142.         else if (sscanf(line, "static char %s = {", name_and_type) == 1)
  143.         {
  144.             version10p = 0;
  145.             break;
  146.         }
  147.         else if (sscanf(line, "static unsigned char %s = {", name_and_type) == 1)
  148.         {
  149.             version10p = 0;
  150.             break;
  151.         }
  152.         else
  153.             continue;
  154.     }
  155.     if (xpmformat)
  156.     {
  157. //        dataP = ReadXpmPixmap(fp, datafile, w, h, colrs, Ncolors, charspp);
  158. //        return(dataP);
  159.                 return(NULL);
  160.     }
  161.     if (*w == 0)
  162.     {
  163.         /* fprintf(stderr, "Can't read image.\n"); */
  164.         return((unsigned char *)NULL);
  165.     }
  166.     if (*h == 0)
  167.     {
  168.         /* fprintf(stderr, "Can't read image.\n"); */
  169.         return((unsigned char *)NULL);
  170.     }
  171.     padding = 0;
  172.     if (((*w % 16) >= 1)&&((*w % 16) <= 8)&&version10p)
  173.     {
  174.         padding = 1;
  175.     }
  176.     bytes_per_line = ((*w + 7) / 8) + padding;
  177.     raster_length =  bytes_per_line * *h;
  178.     dataP = (unsigned char *)malloc((*w) * (*h));
  179.     if (dataP == NULL)
  180.     {
  181.         fprintf(stderr, "Not enough memory.\n");
  182.         return((unsigned char *)NULL);
  183.     }
  184.     ptr = dataP;
  185.     if (version10p)
  186.     {
  187.         int cnt = 0;
  188.         int lim = (bytes_per_line - padding) * 8;
  189.         for (bytes = 0; bytes < raster_length; bytes += 2)
  190.         {
  191.             if (fscanf(fp, " 0x%x%*[,}]%*[ \r\n]", &value) != 1)
  192.             {
  193.                 fprintf(stderr, "Error scanning bits item.\n");
  194.                 return((unsigned char *)NULL);
  195.             }
  196.             temp = value;
  197.             value = temp & 0xff;
  198.             for (i = 0; i < 8; i++)
  199.             {
  200.                 if (cnt < (*w))
  201.                 {
  202.                     if (value & nibMask[i])
  203.                         *ptr++ = 1;
  204.                     else
  205.                         *ptr++ = 0;
  206.                 }
  207.                 if (++cnt >= lim)
  208.                     cnt = 0;
  209.             }
  210.             if ((!padding)||((bytes+2) % bytes_per_line))
  211.             {
  212.                 value = temp >> 8;
  213.                 for (i = 0; i < 8; i++)
  214.                 {
  215.                     if (cnt < (*w))
  216.                     {
  217.                         if (value & nibMask[i])
  218.                             *ptr++ = 1;
  219.                         else
  220.                             *ptr++ = 0;
  221.                     }
  222.                     if (++cnt >= lim)
  223.                         cnt = 0;
  224.                 }
  225.             }
  226.         }
  227.     }
  228.     else
  229.     {
  230.         int cnt = 0;
  231.         int lim = bytes_per_line * 8;
  232.         for (bytes = 0; bytes < raster_length; bytes++)
  233.         {
  234.             if (fscanf(fp, " 0x%x%*[,}]%*[ \r\n]", &value) != 1)
  235.             {
  236.                 fprintf(stderr, "Error scanning bits item.\n");
  237.                 return((unsigned char *)NULL);
  238.             }
  239.             *ptr++ = value;
  240.         }
  241.     }
  242.     return(dataP);
  243. }
  244.  
  245.  
  246.